<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员等级" prop="memberLevelType">
        <el-select v-model="queryParams.memberLevelType" placeholder="请选择会员等级" clearable
          @keyup.enter.native="handleQuery">
          <el-option v-for="item in DictData.memberList" :key="item.value" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:level:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:level:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:level:remove']">删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:level:export']">导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="levelList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="会员等级" align="center" prop="memberLevelType" />
      <el-table-column label="等级名称" align="center" prop="memberLevelName">
        <template slot-scope="{ row }">
          {{ forrmatterEnmu(DictData.memberList, row.memberLevelType) }}
        </template>
      </el-table-column>

      <el-table-column label="升级次数" align="center" prop="upgradeNumber" />

      <!-- <el-table-column
        label="直推下属奖"
        align="center"
        prop="directPremium"
        width="130"
      />
      <el-table-column
        label="平台补贴"
        align="center"
        prop="recommendedRoom"
        width="80"
      />
      <el-table-column
        label="团队奖"
        align="center"
        prop="teamAward"
        width="150"
      />
      <el-table-column
        label="代理推平级奖"
        align="center"
        prop="agencyGradeAward"
        width="130"
      />
      <el-table-column
        label="总裁推共享奖"
        align="center"
        prop="bossShareAward"
        width="130"
      />
      <el-table-column
        label="总裁推联创奖"
        align="center"
        prop="bossLianChangAward"
        width="130"
      />
      <el-table-column
        label="区域运维推共享店主奖"
        align="center"
        prop="areaShareAward"
        width="150"
      />
      <el-table-column
        label="共享推联创奖"
        align="center"
        prop="shareLianChangAward"
        width="130"
      />
      <el-table-column
        label="共享推运营奖"
        align="center"
        prop="shareAreaAward"
        width="130"
      />
      <el-table-column
        label="共享推总裁奖"
        align="center"
        prop="shareBossAward"
        width="130"
      />
      <el-table-column
        label="联创推运营奖"
        align="center"
        prop="lianChangAreaAward"
        width="130"
      />
      <el-table-column
        label="联创推boss奖"
        align="center"
        prop="lianChangBossAward"
        width="150"
      />
      <el-table-column
        label="区域运营推总裁奖"
        align="center"
        prop="areaBossAward"
        width="150"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:level:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:level:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员等级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-row :gutter="20">
          <el-col :span="24" :offset="0">
            <el-form-item label="会员等级" prop="memberLevelType">
              <el-select
                v-model="form.memberLevelType"
                placeholder="请选择等级名称"
                clearable
                @change="memberLevelTypeChange"
                style="width: 100%"
              >
                <el-option
                  v-for="item in DictData.memberList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col
            :span="24"
            :offset="0"
            v-if="
              form.memberLevelType == 2 ||
              form.memberLevelType == 4 ||
              form.memberLevelType == 5 ||
              form.memberLevelType == 6
            "
          >
            <el-form-item label="升级次数" prop="upgradeNumber">
              <el-input
                v-model="form.upgradeNumber"
                placeholder="请输入升级次数"
              />
            </el-form-item>
          </el-col>

          <!-- <el-col
            :span="24"
            :offset="0"
            v-show="form.memberLevelType == 1 || form.memberLevelType == 2"
          >
            <el-form-item label="直推下属奖" prop="directPremium">
              <el-input
                v-model="form.directPremium"
                placeholder="请输入直推下属奖"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 2">
            <el-form-item label="平台补贴" prop="recommendedRoom">
              <el-input
                v-model="form.recommendedRoom"
                placeholder="请输入平台补贴"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            :offset="0"
            v-show="
              form.memberLevelType == 4 ||
              form.memberLevelType == 5 ||
              form.memberLevelType == 6
            "
          >
            <el-form-item label="团队奖" prop="teamAward">
              <el-input v-model="form.teamAward" placeholder="请输入团队奖">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            :offset="0"
            v-show="
              form.memberLevelType == 3 ||
              form.memberLevelType == 4 ||
              form.memberLevelType == 5 ||
              form.memberLevelType == 6
            "
          >
            <el-form-item label="代理推平级奖" prop="agencyGradeAward">
              <el-input
                v-model="form.agencyGradeAward"
                placeholder="请输入代理推平级奖"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 6">
            <el-form-item label="总裁推共享奖" prop="bossShareAward">
              <el-input
                v-model="form.bossShareAward"
                placeholder="请输入总裁推共享奖"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 6">
            <el-form-item label="总裁推联创奖" prop="bossLianChangAward">
              <el-input
                v-model="form.bossLianChangAward"
                placeholder="请输入总裁推联创奖"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 6">
            <el-form-item label="总裁推区域运营" prop="directPremium">
              <el-input
                v-model="form.directPremium"
                placeholder="请输入总裁推区域运营"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 5">
            <el-form-item label="区域运营推共享店主奖" prop="areaShareAward">
              <el-input
                v-model="form.areaShareAward"
                placeholder="请输入区域运营推共享店主奖"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 5">
            <el-form-item label="区域运营推联创奖" prop="directPremium">
              <el-input
                v-model="form.directPremium"
                placeholder="请输入区域运营推联创奖"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 5">
            <el-form-item label="区域运营推总裁奖" prop="areaBossAward">
              <el-input
                v-model="form.areaBossAward"
                placeholder="请输入区域运营推总裁奖"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 4">
            <el-form-item label="联创推共享店主奖" prop="directPremium">
              <el-input
                v-model="form.directPremium"
                placeholder="请输入联创推共享店主奖"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 4">
            <el-form-item label="联创推区域运营奖" prop="lianChangAreaAward">
              <el-input
                v-model="form.lianChangAreaAward"
                placeholder="请输入联创推区域运营奖"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 4">
            <el-form-item label="联创推总裁奖" prop="lianChangBossAward">
              <el-input
                v-model="form.lianChangBossAward"
                placeholder="请输入联创推总裁奖"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 3">
            <el-form-item label="共享推联创奖" prop="shareLianChangAward">
              <el-input
                v-model="form.shareLianChangAward"
                placeholder="请输入共享推联创奖"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 3">
            <el-form-item label="共享推区域运营奖" prop="shareAreaAward">
              <el-input
                v-model="form.shareAreaAward"
                placeholder="请输入共享推区域运营奖"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0" v-show="form.memberLevelType == 3">
            <el-form-item label="共享推总裁奖" prop="shareBossAward">
              <el-input
                v-model="form.shareBossAward"
                placeholder="请输入共享推总裁奖"
              />
            </el-form-item>
          </el-col> -->

          <el-col :span="24" :offset="0">
            <el-form-item label="团队奖百分比" prop="shareBossAward">
              <el-input
                v-model="form.teamAward"
                placeholder="请输入团队奖百分比"
              >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="16" :offset="0">
            <el-form-item label="商品信息" prop="goodId">
              <el-button type="primary" plain @click="goodOpen = true"
                >选择商品</el-button
              >
              <div class="goods-container">
                <div
                  class="goods-item"
                  v-for="(item, index) in productList"
                  :key="item.id"
                >
                  <div class="goods-item-left">
                    <img :src="baseURL + item.goodImg" alt="" />
                    <div class="good-item-title">
                      <div class="good-title-name">
                        {{ item.goodName }}
                      </div>
                      <div>￥ {{ item.retailPrice }}</div>
                    </div>
                    <el-button
                      icon="el-icon-delete"
                      plain
                      @click="goodsSelectionDelete(index)"
                      >删除</el-button
                    >
                  </div>
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

      <!-- 选择商品列表 -->
      <el-dialog
        title="绑定会员等级商品"
        :visible.sync="goodOpen"
        width="800px"
        append-to-body
        v-if="goodOpen"
      >
        <GoodList @tableSelection="tableSelection"></GoodList>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLevel,
  getLevel,
  delLevel,
  addLevel,
  updateLevel,
} from "@/api/system/member/level";
import { listGood } from "@/api/system/goods/list";
import GoodList from "./components/goodList.vue";
import { forrmatterEnmu } from "@/utils/index";

export default {
  name: "Level",
  components: { GoodList },
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback();
      }
      setTimeout(() => {
        if (!Number.isInteger(value - 0)) {
          callback(new Error("请输入数字值"));
        } else {
          callback();
        }
      }, 1000);
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员等级表格数据
      levelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodId: null,
        memberLevelType: null,
        memberLevelName: null,
        recommendedRoom: null,
        teamAward: null,
        // lowAward: null,
        directPremium: null,
        agencyGradeAward: null,
        bossShareAward: null,
        bossLianChangAward: null,
        areaShareAward: null,
        commentGrowthPoint: null,
        freight: null,
        note: null,
        createUser: null,
        updateUser: null,
        isDelete: null,
        shareLianChangAward: null,
        shareAreaAward: null,
        shareBossAward: null,
        lianChangAreaAward: null,
        lianChangBossAward: null,
        areaBossAward: null,
        memberLevelType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberLevelType: {
          required: true,
          message: "会员等级必填",
          trigger: "blur",
        },
        upgradeNumber: { validator: true, trigger: "blur" },

        directPremium: [{ validator: checkAge, trigger: "blur" }],
        recommendedRoom: [{ validator: checkAge, trigger: "blur" }],
        teamAward: [{ validator: checkAge, trigger: "blur" }],
        // lowAward: [{ validator: checkAge, trigger: "blur" }],
        agencyGradeAward: [{ validator: checkAge, trigger: "blur" }],
        bossShareAward: [{ validator: checkAge, trigger: "blur" }],
        bossLianChangAward: [{ validator: checkAge, trigger: "blur" }],
        areaShareAward: [{ validator: checkAge, trigger: "blur" }],
        shareLianChangAward: [{ validator: checkAge, trigger: "blur" }],
        shareAreaAward: [{ validator: checkAge, trigger: "blur" }],
        shareBossAward: [{ validator: checkAge, trigger: "blur" }],
        lianChangAreaAward: [{ validator: checkAge, trigger: "blur" }],
        lianChangBossAward: [{ validator: checkAge, trigger: "blur" }],
        areaBossAward: [{ validator: checkAge, trigger: "blur" }],
      },
      goodOpen: false,
      productList: [],
      baseURL: process.env.VUE_APP_BASE_API,
      goodList: [],
    };
  },
  created() {
    this.getList();
    this.getGoodsList();
  },
  methods: {
    forrmatterEnmu,
    /** 查询会员等级列表 */
    getList() {
      this.loading = true;
      listLevel(this.queryParams).then((response) => {
        this.levelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询供应商商品列表 */
    getGoodsList() {
      let params = {
        pageSize: 100,
        pageNum: 1,
      };
      listGood(params).then((response) => {
        this.goodList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodId: null,
        memberLevelType: null,
        memberLevelName: null,
        recommendedRoom: null,
        teamAward: null,
        // lowAward: null,
        directPremium: null,
        agencyGradeAward: null,
        bossShareAward: null,
        bossLianChangAward: null,
        areaShareAward: null,
        commentGrowthPoint: null,
        freight: null,
        note: null,
        createUser: null,
        updateUser: null,
        createTime: null,
        updateTime: null,
        teamAward: null,
        isDelete: null,
        shareLianChangAward: null,
        shareAreaAward: null,
        shareBossAward: null,
        lianChangAreaAward: null,
        lianChangBossAward: null,
        areaBossAward: null,
        memberLevelType: null,
        singlePrize: null,
        upgradeNumber: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.productList = [];
      this.open = true;
      this.title = "添加会员等级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.productList = [];
      const id = row.id || this.ids;
      getLevel(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员等级";
        let goodids = response.data.goodId.split(",");
        let list = [];
        this.goodList.forEach((item) => {
          goodids.forEach((i) => {
            if (i == item.id) {
              list.push(item);
            }
          });
        });
        this.productList = list;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLevel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLevel(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return delLevel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/level/export",
        {
          ...this.queryParams,
        },
        `level_${new Date().getTime()}.xlsx`
      );
    },
    // 选择商品
    tableSelection(v) {
      this.form.goodId = v.map((item) => item.id).join(",");
      this.productList = v;
      this.goodOpen = false;
    },
    // 删除选择的商品
    goodsSelectionDelete(index) {
      this.productList.splice(index, 1);
    },
    memberLevelTypeChange(v) {
      this.reset();
      this.form.memberLevelType = v;
      this.productList = [];
    },
  },
};
</script>
<style lang="less" scoped>
.goods-container {
  .goods-item {
    height: 80px;
    // width: 100%;
    width: 300px;
    margin-top: 20px;
    padding: 0 10px;
    border: 1px solid #c9c9c9;
    display: flex;
    border-radius: 10px;

    .goods-item-left {
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;

      > img {
        height: 60px;
        width: 60px;
      }

      .good-item-title {
        // flex: 1;
        // display: flex;
        width: 200px;

        line-height: 20px;
        .good-title-name {
          width: 90px; /* 固定宽度 */
          display: -webkit-box; /* 必须结合 -webkit-box 使用 */
          -webkit-box-orient: vertical; /* 设置盒子垂直排列子元素 */
          -webkit-line-clamp: 2; /* 限制显示两行 */
          overflow: hidden; /* 隐藏溢出的内容 */
          text-overflow: ellipsis; /* 显示省略号 */
          white-space: normal;
        }

        flex-direction: column;
        justify-content: space-between;
        margin: 10px 20px;
      }
    }
  }
}
</style>
